<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>子表数据授权</title>
    <#include "/pubHeadStyleTree.html">
    <script type="text/javascript" src="${ctx}/workflowStatic/js/javacode/codemirror.js"></script>
    <script type="text/javascript" src="${ctx}/workflowStatic/js/javacode/InitMirror.js"></script>
    <script type="text/javascript" src="${ctx}/workflowStatic/js/commonScript.js"></script>
    <script type="text/javascript" src="${ctx}/workflowStatic/js/SubRightsTree.js"></script>
    <link href="${ctx}/workflowStatic/style/css/workflow.css" rel="stylesheet">
</head>
<body>

<div style="position: relative;height: 100%;">
    <div class="ccpui-trees" >
        <p class="isShowTree colorSwitch">
            <span style="float: left;color: #fff">&nbsp;&nbsp;子表</span>
            <i layadmin-event="treeFlexible" class="layui-icon layui-icon-prev"></i>
        </p>
        <div style="margin-top: 6px;">
            <ul id="subTree" class="ztree ccpui-listZtree"></ul>
        </div>
    </div>
    <div  class="ccpui-trees" style="position: absolute;top:10px;right:10px;bottom: 0;left: 250px; width: 60%;">
        <p class="isShowTree colorSwitch">
            <span style="float: left;color: #fff">&nbsp;&nbsp;子表授权设置</span>
        </p>
        <div style="padding:10px 10px 10px 10px ">
            <a class="layui-btn layui-btn-sm" id="dataFormSave" href="javascript:;"><i class="iconfont iconicon_baocun"></i><span>保存</span></a>
            <a class="layui-btn layui-btn-danger layui-btn-sm" id="dataFormDel" href="javascript:;"><i class="iconfont iconshanchu"></i><span>删除权限</span></a>
            <a class="layui-btn layui-btn-sm" id="dataFormClose" href="javascript:;"><i class="iconfont iconguanbi"></i><span>关闭</span></a>
        </div>
        <form id="subAuthorityForm" class="layui-form" action="" lay-filter="subAuthorityForm">

            <input type="hidden" id="rightId" name="id" value="0">
            <input type="hidden" id="procDefId" name="procDefId" value="${procDefId}">
            <input type="hidden" id="nodeId" name="nodeId" value="${nodeId}">
            <input type="hidden" id="mainTableId" name="mainTableId" value="${tableId}">
            <input type="hidden" id="tableId" name="tableId" value="0">
            <input type="hidden" id="parentProcDefId" name="parentProcDefId" value="${parentProcDefId}">

            <div class="header">
                <div class="layui-form-item">
                    <span class="difcolor" style="float: left;margin-left: 10px; line-height: 40px;">条件类型：</span>
                    <span class="difcolor" style="float: left;margin-left: 10px">
							<input id="condition-user" value="0" title="当前人员" type="radio" name="permissionType" lay-filter="permissionType" checked="checked">
						</span>
                    <span class="difcolor" style="float: left;margin-left: 20px">
							<input id="condition-org" value="1" title="当前组织" type="radio" name="permissionType" lay-filter="permissionType">
						</span>
                    <span class="difcolor" style="float: left;margin-left: 20px">
							<input id="condition-script" value="2" title="脚本权限" type="radio" name="permissionType" lay-filter="permissionType">
						</span>
                </div>
            </div>

            <div class="panel-body">

                <div style="margin-top:5px;margin-bottom:5px;">
                    <font color="red">单击子表父节点选取操作子表；</font>
                </div>

                <table border="0">
                    <tr>
                        <td><div style="margin-top:5px;margin-bottom:5px;">当前操作子表为：</div></td>
                        <td><div id="subName" style="margin-top:5px;margin-bottom:5px;"></div></td>
                    </tr>
                </table>

                <div id="scriptAuthority">
                    <div class="condition-script-div-script">
                        <table class="table-detail">
                            <tr>
                                <th width="20%" >条件脚本: </th>
                                <td>
                                    <div>
                                        <span class="commonScript" style="cursor: pointer;">
                                            <i class="layui-icon layui-icon-fonts-code"></i>常用脚本
                                        </span>
                                        <font color="red">双击选取子表字段编写脚本；</font>
                                        <div class="text-adjust">
                                            <a href="javascript:;" id="tipInfo">
                                                <i class="iconfont iconicon_yiwenkongxin" style="font-size: 20px"></i>
                                            </a>
                                        </div>
                                    </div>
                                    <textarea name="script" codemirror="true" mirrorheight="200px" rows="10" cols="80" >return sql;</textarea>
                                </td>

                            </tr>
                        </table>
                    </div>
                </div>
            </div>

        </form>
    </div>
</div>

<script>
    layui.config({
        base: serverPath.systemPath + staticPath.systemPath //静态资源所在路径
    }).extend({
//        index: 'index' //主入口模块
    }).use(['index','setter','layer', 'form','jquery','table','laytpl','commons'], function() {
        var layer = layui.layer
            , $ = layui.jquery
            , form = layui.form
            , setter = layui.setter
            , table = layui.table
            , laytpl = layui.laytpl
            , commons = layui.commons
            , element = layui.element;

        var nodeId="${nodeId}";
        var procDefId="${procDefId}";
        var formKey="${formKey}";
        var tableId="${tableId}";

        // 选择：当前人员，当前组织，脚本权限
        window.changeUserScript = function(value){
            if(value==0||value==1){
                $('#scriptAuthority').hide();
            }
            if(value==2){
                $('#scriptAuthority').show();
            }
        };

        form.on('radio(permissionType)',function (data) {
            changeUserScript(data.value);
            form.render();
        });

        changeUserScript($('input[name="permissionType"]:checked').val());

        //提示信息
        $('#tipInfo').hover(function () {
            layer.tips('例1：<p>String sql="and a.f_spmc=\'"+scriptImpl.getCurrentName()+"\'";</p>\n' +
                '<p>return sql;</p>\n' +
                '例2：<p>String sql="or a.f_spmc=\'"+scriptImpl.getCurrentName()+"\'";</p>\n' +
                '<p>return sql;</p>', '#tipInfo',{area:['300px', 'auto']});
        },function () {
            layer.tips.close
        });

        // 选择：当前人员，当前组织，脚本权限
        window.changeUserScript = function(value){
            if(value==0||value==1){
                $('#scriptAuthority').hide();
            }
            if(value==2){
                $('#scriptAuthority').show();
            }
        };


        /**
         * 单击节点
         */
        function singleClickHandler(treeId,treeNode) {
            if(typeof(treeId)=="undefined")
                return ;

            if (treeNode.level == 0 && treeNode.id!='-1') {

                $('#subName').text(treeNode.name);
                $('#tableId').val(treeNode.id);

                $.ajax({
                    type : "POST",
                    url : serverPath.workflowPath + setter.workflowLinkPath + "subTableRights/detail",
                    data: {procDefId:$('#procDefId').val(), nodeId:$('#nodeId').val(),
                        tableId:treeNode.id, parentActDefId:$('#parentProcDefId').val()},
                    dataType:"json",
                    success : function(res) {
                        var result = eval('('+res.data+')');
                        var id = typeof(result.permissionType)!="undefined"?result.id:'0';
                        $('#rightId').val(id);
                        if(typeof(result.permissionType)!="undefined"){
                            $('input[name=permissionType][value='+result.permissionType+']').attr("checked",true);
                            $('#scriptAuthority').hide();
                            if(result.permissionType==2){
                                $('#scriptAuthority').show();
                                InitMirror.editor.setCode(result.permissionSetting.replaceAll('<br>','\n').replaceAll('<032>','\"'));
                            }
                        }
                    },
                    error : function(res) {

                    }
                });
            }
        }

        /**
         * 双击节点
         */
        function onDbClick(treeId,treeNode) {

            if(typeof(treeId)=="undefined")
                return ;

            if (treeNode.level != 0)
            {
                if($('#subName').text()==treeNode.getParentNode().name)
                {
                    InitMirror.editor.insertCode(treeNode.name);
                }
                else if(treeNode.getParentNode().id=="-1"){
                    InitMirror.editor.insertCode(treeNode.name);
                }
                else
                {
                    $.ligerDialog.warn('选取的子表字段不属于当前操作子表','提示信息');
                }
            }
        };

        /**
         * 显示是否有权限时的图标
         */
        function changeShow(subTree,node,state){
            if(!node.isParent)
                node=node.getParentNode();
            var showname=node.showName;
            var iconFolder = __ctx + '/styles/tree/';
            var lastIndex =-1;
            var imgurl='';
            if(state=="1"){
                lastIndex=showname.lastIndexOf("<img src='"+iconFolder+"yes.png'>");
                imgurl="<img src='"+iconFolder+"no.png'>"
            }else{
                lastIndex=showname.lastIndexOf("<img src='"+iconFolder+"no.png'>");
                imgurl="<img src='"+iconFolder+"yes.png'>"
            }
            if(lastIndex!=-1){
                var newname=showname.substring(0,lastIndex)+imgurl;
                node.showName=newname;
                subTree.updateNode(node);
            }
        }

        var subTree = new SubRightsTree("subTree", {
            url : "/form/table/getSubTree",
            params : {
                tableId:tableId,
                nodeId:nodeId,
                procDefId:procDefId,
                parentProcDefId:$("#parentProcDefId").val(),
                onClick: singleClickHandler,
                onDbClick: onDbClick
            }
        });
        subTree.loadTree();

        $("#dataFormSave").click(function() {

            if($('#subName').text()==''){
                layer.alert("请选取子表配置权限");
                return;
            }

            InitMirror.editor.save();

            var frm=$('#subAuthorityForm');
            var permissionType = $('input[name="permissionType"]:checked').val();
            if(permissionType==2&&!frm.valid()) return ;
            var procDefId = $('#procDefId').val();
            var nodeId = $('#nodeId').val();
            var tableId = $('#tableId').val();
            var parentProcDefId = $("#parentProcDefId").val();
            var permissionSetting = permissionType==2?$('#permissionSetting').val().trim():'';


            var url=serverPath.workflowPath + setter.workflowLinkPath + "subTableRights/add";

            var data={};
            data.procDefId=procDefId;
            data.nodeId=nodeId;
            data.tableId=tableId;
            data.permissionType=permissionType;
            data.permissionSetting=permissionSetting;
            data.parentProcDefId = parentProcDefId;


            $.post(url,data,function(res){
                if(res.code==0){
                    var subTree = $.fn.zTree.getZTreeObj("subTree");
                    var node = subTree.getSelectedNodes()[0];
                    changeShow(subTree,node,0);
                    singleClickHandler(node.id,node);
                    layer.alert(res.msg);
                }else{
                    layer.alert(res.msg);
                }
            },"json");

        });

        $("#dataFormDel").click(function() {
            if($('#rightId').val()!=0){
                var procDefId = $('#procDefId').val();
                var nodeId = $('#nodeId').val();
                var tableId = $('#tableId').val();

                var url=serverPath.workflowPath + setter.workflowLinkPath + "subTableRights/delete";

                var data={};
                data.id = $('#rightId').val();
                data.procDefId=procDefId;
                data.nodeId=nodeId;
                data.tableId=tableId;

                $.post(url,data,function(res){
                    if(res.code==0){
                        var subTree = $.fn.zTree.getZTreeObj("subTree");
                        var node = subTree.getSelectedNodes()[0];
                        changeShow(subTree,node,1);
                        singleClickHandler(node.id,node);
                        layer.alert(res.msg);
                    }else{
                        layer.alert(res.msg);
                    }
                },"json");
            }else{
                layer.alert("权限不存在不能删除");
            }
        });

        $("#dataFormClose").click(function() {
            parent.layer.closeAll();
        });

    });
</script>

</body>
</html>